import Vue from 'vue'
import Router from 'vue-router'
import Nofind from './components/404'
import Login from './views/Login'
import Index from './views/Index'
import Home from './views/Home'
import Inquiry from './views/Inquiry'
import commodity from './views/commodity'
import InquiryCreate from './views/Inquiry/inquiryCreate'
import inquiryDetail from './views/Inquiry/inquiryDetail'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    { path: '*', name: '/404', component: Nofind },
    { path: '/', redirect: '/index' },
    {
      path: '/index',
      component: Index,
      children: [
        { path: '', component: Home },
        { path: '/home', name: 'home', component: Home },
        { path: '/inquiry', name: 'inquiry', component: Inquiry },
        { path: '/commodity', name: 'commodity', component: commodity },
        {
          path: '/Inquiry/inquiryCreate',
          name: 'inquiryCreate',
          component: InquiryCreate
        },
        {
          path: '/Inquiry/inquiryDetail/:id',
          name: 'inquiryDetail',
          component: inquiryDetail
        }
      ]
    },
    { path: '/login', name: 'login', component: Login }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.antToken ? true : false
  if (to.path == '/login') {
    next()
  } else {
    isLogin ? next() : next('/login')
  }
})

export default router
